var app = getApp();

Page({
  data: {
    screen_w: 320,
    screen_h: 480,
    latitude: '',
    longitude: '',
    markers: ''
  },

  marker: {},
  myMarkers: [],

  locationLat: 0,
  locationLon: 0,

  onLoad: function (data) {
    this.setData({
      screen_w: app.screen_w,
      screen_h: app.screen_h
    });
    console.log("onLoad params:", data)
    
    this.marker.id = 1
    this.marker.latitude = parseFloat(data.lat)
    this.marker.longitude = parseFloat(data.lon)
    this.marker.title = data.name
    this.marker.width = 32
    this.marker.height = 38
    this.marker.iconPath = "/images/poisign.png"
    this.myMarkers = this.myMarkers.concat(this.marker)

  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setCenter()
    var that = this
    setTimeout(() => {
      that.setCenter()
    }, 1000);
  },

  setCenter: function() {
    var that = this
    app.getLocation(function(res) {
      if (res.latitude && res.longitude) {  
        that.locationLat = res.latitude
        that.locationLon = res.longitude
        let centerLat = ((res.latitude + that.marker.latitude) / 2).toFixed(5)
        let centerLon = ((res.longitude + that.marker.longitude) / 2).toFixed(5)
        console.log("中心经纬度：", "centerLat = " + centerLat + ", centerLon = " + centerLon)
        that.setData({
          latitude: centerLat,
          longitude: centerLon,
          markers: that.myMarkers
        })
        that.setScale()
      }
    })
  },

  setScale: function() {
    var that = this
  
    this.mapCtx = tt.createMapContext('myMap')
    
    this.mapCtx.includePoints({
      padding: [50, 50, 50, 50],
      points: [{
        id: 1,
        latitude: that.marker.latitude,
        longitude: that.marker.longitude
      }, {
        id: 2,
        latitude: that.locationLat,
        longitude: that.locationLon
      }],
      success(res) {
        console.log("缩放成功, ",res);
        
      },
      fail(res) {
        console.log("缩放失败: ",res);
      },
      complete(res) {
        console.log("接口已调用: ",res.errMsg);
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  }

  
})